﻿using System;
using System.Data.OleDb;
using System.Windows.Forms;

namespace Texpro
{
    public partial class FMMN203 : Form
    {
        
        public FMMN203()
        {
            InitializeComponent();
        }

        private bool update_user_password(string p_user_id, string p_user_password)
        {
            String strQuery = String.Empty;
            OleDbConnection sqlConn = new OleDbConnection();
            OleDbCommand sqlComm = new OleDbCommand();
            //
            strQuery = String.Empty;
            strQuery += "UPDATE [tis_user] SET [user_password] = '" + p_user_password + "' WHERE [user_id] =  '" + p_user_id + "';";
            sqlConn.ConnectionString = PCPUB.m_oledb_connection.ToString();
            sqlConn.Open();
            sqlComm.Connection = sqlConn;
            sqlComm.CommandText = strQuery.ToString();
            sqlComm.ExecuteNonQuery();
            return true;
        }

        private void frm_change_password_Load(object sender, EventArgs e)
        {
            lbl_form_title.Text = this.Text;
            clear_fields();
            txt_password.Focus();
           // UseWaitCursor = false;
        }

        private bool validate_fields()
        {
            bool b_error = false;
            ep_form.Clear();
            if (txt_password.Text == String.Empty)
            {
                ep_form.SetError(txt_password, "Password field should not be empty.");
                b_error = true;
            }
            if (txt_confirm_password.Text == String.Empty)
            {
                ep_form.SetError(txt_confirm_password, "Confirm Password field should not be empty.");
                b_error = true;
            }
            else if (txt_password.Text !=  txt_confirm_password.Text)
            {
                ep_form.SetError(txt_confirm_password, "Password and Confirm Password fields should be same.");
                b_error = true;
            }
            if (b_error)
            {
                PCMSG.Show("Error(s) occured. Please check and clear errors.","Change Password",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            if (ep_form.GetError(txt_password) != String.Empty)
            {
                txt_password.Focus();
                return false;
            }
            else if (ep_form.GetError(txt_confirm_password) != String.Empty)
            {
                txt_confirm_password.Focus();
                return false;
            }
            else
            {
                return true;
            }
        }

        private void tsb_cancel_Click(object sender, EventArgs e)
        {
            clear_fields();
            this.Close();
        }

        private void tsb_ok_Click(object sender, EventArgs e)
        {
           // UseWaitCursor = true;
            if (validate_fields())
            {
                string user_id = PCPUB.m_user_id;
                if (PCPUB.m_form_option_code == "RESET_USER_PASSWORD")
                    user_id = PCPUB.m_admin_user_id;
                string user_password = txt_confirm_password.Text.Trim().ToUpper();
                if (update_user_password(user_id, user_password))
                {
                    if (PCPUB.m_form_option_code == "RESET_USER_PASSWORD")
                        PCMSG.Show("Reset Password completed successfully.", "Reset Password", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    else
                        PCMSG.Show("Change Password completed successfully.", "Change Password", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
            }
           // UseWaitCursor = false;
        }

        private void clear_fields()
        {
            txt_password.Text = String.Empty;
            txt_confirm_password.Text = String.Empty;
            ep_form.Clear();
        }

        private void txt_user_id_Validated(object sender, EventArgs e)
        {
            //if (txt_password.Text == String.Empty)
            //{
            //    ep_form.SetError(txt_password, "UserId field should not be empty.");
            //}
        }

        private void txt_user_password_Validated(object sender, EventArgs e)
        {
            //if (txt_confirm_password.Text == String.Empty)
            //{
            //    ep_form.SetError(txt_confirm_password, "Password field should not be empty.");
            //}
            //else if (txt_password.Text != txt_confirm_password.Text)
            //{
            //    ep_form.SetError(txt_confirm_password, "Password and Confirm Password fields should be same.");
            //}
        }

        private void text_id_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_gen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsLetterOrDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

        private void text_int_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back))
            {
                e.Handled = true;
            }
        }

        private void text_dec_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsDigit(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == '.'))
            {
                e.Handled = true;
            }
        }

    }
}